package com.springbootmybatis.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springbootmybatis.pojo.Book;
import com.springbootmybatis.service.BookService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class BookController {
	@Resource
	BookService bookService;
	@GetMapping("/books")
	public String findAllBook(Model model){
		List<Book> books = bookService.findAllBook();
		model.addAttribute("books",books);
		return "/bookList";
	}
	@GetMapping("/book/{id}")
	public String findBookById(@PathVariable Integer id,Model model){
		Book book = bookService.findBookById(id);
		model.addAttribute("book",book);
		return "/book";
	}
	@GetMapping("/searchBooks")
	public String searchBooks(Book book,Model model){
		List<Book> books = bookService.searchBooks(book);
		model.addAttribute("books",books);
		return "/bookList";
	}
	@GetMapping("/booksPage")
	public String booksPage(@RequestParam(defaultValue = "1")Integer start,@RequestParam(defaultValue = "3")Integer size,Model model){
		PageHelper.startPage(start,size,"id asc");
		List<Book>books=bookService.findAllBook();
		PageInfo<Book> page=new PageInfo<>(books);
		model.addAttribute("page",page);
		return "/bookListPage";
	}
}
